package com.base.card.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.base.common.core.page.TableDataInfo;
import com.base.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.base.card.domain.TCryptCardKeyHistory;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.base.common.utils.StringUtils;
import com.base.card.domain.bo.TCryptCardKeyHistoryBo;
import com.base.card.domain.vo.TCryptCardKeyHistoryVo;
import com.base.card.mapper.TCryptCardKeyHistoryMapper;
import com.base.card.service.ITCryptCardKeyHistoryService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 密码卡密钥历史Service业务层处理
 *
 * @author base
 * @date 2023-10-17
 */
@RequiredArgsConstructor
@Service
public class TCryptCardKeyHistoryServiceImpl implements ITCryptCardKeyHistoryService {

    private final TCryptCardKeyHistoryMapper baseMapper;

    /**
     * 查询密码卡密钥历史
     */
    @Override
    public TCryptCardKeyHistoryVo queryById(String autoId){
        return baseMapper.selectVoById(autoId);
    }

    /**
     * 查询密码卡密钥历史列表
     */
    @Override
    public TableDataInfo<TCryptCardKeyHistoryVo> queryPageList(TCryptCardKeyHistoryBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TCryptCardKeyHistory> lqw = buildQueryWrapper(bo);
        Page<TCryptCardKeyHistoryVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询密码卡密钥历史列表
     */
    @Override
    public List<TCryptCardKeyHistoryVo> queryList(TCryptCardKeyHistoryBo bo) {
        LambdaQueryWrapper<TCryptCardKeyHistory> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TCryptCardKeyHistory> buildQueryWrapper(TCryptCardKeyHistoryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TCryptCardKeyHistory> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getCardCode()), TCryptCardKeyHistory::getCardCode, bo.getCardCode());
        lqw.eq(bo.getCardIdx() != null, TCryptCardKeyHistory::getCardIdx, bo.getCardIdx());
        lqw.eq(StringUtils.isNotBlank(bo.getKeyCode()), TCryptCardKeyHistory::getKeyCode, bo.getKeyCode());
        lqw.eq(bo.getDistributeTime() != null, TCryptCardKeyHistory::getDistributeTime, bo.getDistributeTime());
        lqw.eq(StringUtils.isNotBlank(bo.getOperator()), TCryptCardKeyHistory::getOperator, bo.getOperator());
        lqw.eq(StringUtils.isNotBlank(bo.getKeySource()), TCryptCardKeyHistory::getKeySource, bo.getKeySource());
        lqw.eq(StringUtils.isNotBlank(bo.getKeyDistributeStatus()), TCryptCardKeyHistory::getKeyDistributeStatus, bo.getKeyDistributeStatus());
        return lqw;
    }

    /**
     * 新增密码卡密钥历史
     */
    @Override
    public Boolean insertByBo(TCryptCardKeyHistoryBo bo) {
        TCryptCardKeyHistory add = BeanUtil.toBean(bo, TCryptCardKeyHistory.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setAutoId(add.getAutoId());
        }
        return flag;
    }

    /**
     * 修改密码卡密钥历史
     */
    @Override
    public Boolean updateByBo(TCryptCardKeyHistoryBo bo) {
        TCryptCardKeyHistory update = BeanUtil.toBean(bo, TCryptCardKeyHistory.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TCryptCardKeyHistory entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除密码卡密钥历史
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
